package com.sso;

import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;

import javax.servlet.ServletException;
import java.io.File;

/**
 * 使用Java语言创建Tomcat服务器<br>
 * Tomcat 底层执行程序 最终servlet<br>
 */
public class MainClient2 {

    public static void main(String[] args) throws LifecycleException, ServletException {
        // 启动 tomcat
        Tomcat tomcat = new Tomcat();
        tomcat.setPort(Integer.getInteger("port", 8887));
        tomcat.getConnector();
        // 创建 WebApp
        Context context = tomcat.addWebapp("/client2", new File("src/main/resources/templates").getAbsolutePath());
        WebResourceRoot resources = new StandardRoot(context);
        resources.addPreResources(
                new DirResourceSet(resources, "/WEB-INF/classes",
                        new File("target/classes").getAbsolutePath(), "/"));
        context.setResources(resources);

        tomcat.start();
        tomcat.getServer().await();
    }

}
